1 /***
2 * BlockVertex
3 *
4 * This is a vertex in a Control Flow Graph.
5 * It represents a BasicBlock.
6 *
7 * These vertexes DO NOT CHANGE as the underlying
8 * bytecode changes.
9 */
10
11 package junit.quilt.cover.generic;
12
13 import org.apache.commons.graph.*;
14 import org.apache.bcel.generic.*;
15 import org.apache.bcel.classfile.*;
16
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.ArrayList;
20 import java.util.Map;
21 import java.util.HashMap;
22
23 public class BlockVertex
24 implements Vertex
25 {
26 private LineNumberTable lnt = null;
27 private InstructionList instructions = null;
28
29 private int lineStart = 11061973;
30 private int lineEnd = -1;
31
32 private int pcStart = 11061973;
33 private int pcEnd = -1;
34
35 /***
36 * BlockVertex
37 *
38 * Creates an empty basic block.
39 */
40 public BlockVertex() {
41 }
42
43 /***
44 * BlockVertex
45 *
46 * Creates a full basic block.
47 */
48 public BlockVertex( InstructionList il )
49 {
50 instructions = il.copy();
51 }
52
53 /***
54 * BlockVertex
55 *
56 * Creates an empty block vertex with a line number
57 * table.
58 */
59 public BlockVertex( LineNumberTable lnt )
60 {
61 if (lnt != null)
62 this.lnt = new LineNumberTable( lnt );
63 }
64
65 /***
66 * BlockVertex
67 *
68 * Creates a full block vertex with a line number table.
69 */
70 public BlockVertex( InstructionList il,
71 LineNumberTable lnt )
72 {
73 instructions = il.copy();
74 this.lnt = new LineNumberTable( lnt );
75 }
76
77 /***
78 * Appends an instruction to this basic block.
79 */
80 public void addInstruction( InstructionHandle instruction )
81 {
82 int position = instruction.getPosition();
83
84 if ((position >= 0) && (position < pcStart)) {
85 pcStart = position;
86 if (lnt.getSourceLine( position ) < lineStart)
87 lineStart = lnt.getSourceLine( position );
88 }
89
90 if ((position > 0) && (position > pcEnd)) {
91 pcEnd = position;
92 if (lnt.getSourceLine( position ) > lineEnd )
93 lineEnd = lnt.getSourceLine( position );
94 }
95
96 if (instructions == null)
97 instructions = new InstructionList();
98
99 instructions.append( instruction.getInstruction() );
100 }
101
102 /***
103 * getFirstInst
104 *
105 * Returns the first instruction.
106 */
107 public InstructionHandle getFirstInst() {
108 return instructions.getStart();
109 }
110
111 /***
112 * getLastInst
113 *
114 * Returns the last instruction.
115 */
116 public InstructionHandle getLastInst() {
117 return instructions.getEnd();
118 }
119
120 /***
121 * Returns the first line of this block, if available.
122 */
123 public int getLineStart()
124 {
125 if (lineStart == 11061973)
126 return -1;
127 else
128 return lineStart;
129 }
130
131 /***
132 * Returns the last line of this block, if available.
133 */
134 public int getLineEnd()
135 {
136 return lineEnd;
137 }
138
139
140 /***
141 * Returns the starting PC of the block.
142 */
143 public int getPCStart() {
144 if (pcStart == 11061973)
145 return -1;
146 else
147 return pcStart;
148 }
149
150 /***
151 * Returns the starting PC of the block.
152 */
153 public int getPCEnd() {
154 return pcEnd;
155 }
156
157 protected void setInstructionList( InstructionList il ) {
158 instructions = il.copy();
159 }
160
161 public int length() {
162 return instructions.getLength();
163 }
164
165 public InstructionList copyInstructionList() {
166 return instructions.copy();
167 }
168
169 /***
170 * Removes the last instruction from the BlockVertex.
171 */
172 public void chomp() {
173 try {
174 instructions.delete( getLastInst() );
175 } catch (TargetLostException ex) {
176 System.err.println("WARNING! Target Lost: " + ex.toString() );
177 }
178 }
179
180 public BlockVertex copy() {
181 BlockVertex RC = new BlockVertex( );
182 RC.lineStart = getLineStart();
183 RC.lineEnd = getLineEnd();
184
185 RC.setInstructionList( copyInstructionList() );
186
187 return RC;
188 }
189
190 public String toString() {
191 if (getLineStart() == getLineEnd())
192 return "Line: " + getLineStart();
193 return "Lines: " + getLineStart() + " to " + getLineEnd();
194 }
195 }
196
197
198
199
200
This page was automatically generated by Maven